<template>
  <!-- 浇注机示意图详情表单弹窗 -->
  <el-dialog
          v-model="visibleDialog"
          :title="props.title"
          width="70%"
          draggable
          @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
      <el-col :span="12">
      <el-form-item label="浇注机时间id" prop="dataId">
      <el-input v-if="!props.isReadonly" v-model="formData.dataId" placeholder="请输入浇注机时间id" clearable />
  
      <span v-else>{{formData.dataId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="产品id" prop="productId">
      <el-input v-if="!props.isReadonly" v-model="formData.productId" placeholder="请输入产品id" clearable />
  
      <span v-else>{{formData.productId}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="产品编号" prop="productCode">
      <el-input v-if="!props.isReadonly" v-model="formData.productCode" placeholder="请输入产品编号" clearable />
  
      <span v-else>{{formData.productCode}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="产品名称" prop="productName">
      <el-input v-if="!props.isReadonly" v-model="formData.productName" placeholder="请输入产品名称" clearable />
  
      <span v-else>{{formData.productName}}</span>
        </el-form-item>
      </el-col>
      <el-col :span="12">
      <el-form-item label="序号" prop="machineIndex">
      <el-input v-if="!props.isReadonly" v-model="formData.machineIndex" placeholder="请输入序号" clearable />
  
      <span v-else>{{formData.machineIndex}}</span>
        </el-form-item>
      </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button v-if="!props.isReadonly" type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
  import request from "@/utils/request";
  import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
  const dictionaryStore = useDictStoreHook();

  defineOptions({
    name: "MESBIPRODUCTLINEDATA81ADDORUPDATE",
    inheritAttrs: false,
  });

  const emit = defineEmits(["reset-table", "change-visible"]);

  const props = defineProps({
    visible: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
    title: {
      type: String,
      default: () => {
        return "";
      },
    },
    data: {
      type: Object,
      default: () => {
        return {};
      },
    },
    isReadonly: {
      type: Boolean,
      default: () => {
        return false;
      },
    },
  });

  const visibleDialog = ref(false);
  watch(
      () => props.visible,
      (newVal: boolean) => {
        visibleDialog.value = newVal;
        setTimeout(() => {
          formRef.value.clearValidate();
        }, 50);
      }
  );

  const rules = reactive({
    dataId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    productId: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    productCode: [
                        { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" }
                  ],
    productName: [
                        { min: 1, max: 90, message: "长度在 1 到 90 个字符", trigger: "blur" }
                  ],
    machineIndex: [
                        ],
  });

  const formRef = ref(ElForm);

  let formData = reactive({
 id: "",  dataId: "",  productId: "",  productCode: "",  productName: "",  machineIndex: "",  version: "",  remarks: "",  createTime: "",  createdUserId: "",  createdUserName: "",  lastUpdateTime: "",  lastUpdatedUserId: "",  lastUpdatedUserName: "",  enabled: "",  deleted: "",   });

  watch(
          () => props.data,
          (newVal: any) => {
            formData.id = ""
            if (newVal?.id) {
              const {  id,  dataId,  productId,  productCode,  productName,  machineIndex,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  } = newVal;
              Object.assign(formData, {  id,  dataId,  productId,  productCode,  productName,  machineIndex,  version,  remarks,  createTime,  createdUserId,  createdUserName,  lastUpdateTime,  lastUpdatedUserId,  lastUpdatedUserName,  enabled,  deleted,  });
            }
          }
  );

  /** 关闭表单弹窗 */
  function closeDialog() {
    visibleDialog.value = false;
    emit("change-visible", false);
    resetForm();
  }

  /** 重置表单 */
  function resetForm() {
    formRef.value.resetFields();
    formRef.value.clearValidate();
    formData.id = ""
    formData.dataId = ""
    formData.productId = ""
    formData.productCode = ""
    formData.productName = ""
    formData.machineIndex = ""
    formData.version = ""
    formData.remarks = ""
    formData.createTime = ""
    formData.createdUserId = ""
    formData.createdUserName = ""
    formData.lastUpdateTime = ""
    formData.lastUpdatedUserId = ""
    formData.lastUpdatedUserName = ""
    formData.enabled = ""
    formData.deleted = ""
  }

  const loading = ref(false);

  /** 浇注机示意图详情保存提交 */
  function handleSubmit() {
    formRef.value.validate(async (valid: any) => {
      if (valid) {
        loading.value = true;
        const text = formData.id ? "编辑" : "新增";
        await request({
          url: "/mes/biproductlinedata81/save",
          method: "post",
          data: formData,
        }).finally(() => {
          loading.value = false;
        });
        ElMessage.success( text +`成功`);
        closeDialog();
        emit("reset-table", formData.id);
      }
    });
  }


  // 组件销毁时调用
  onBeforeUnmount(() => {
    visibleDialog.value = false;
    emit("change-visible", false);
  });


</script>
